package com.sicurezza.nfcrypto;

import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.sicurezza.db.KeyContactContract.KeyContactEntry;

public class ContactViewActivity extends Activity {

	private static final String TAG = "ContactViewActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact_view);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		Intent callerIntent = this.getIntent();
//		String id = callerIntent.getStringExtra(KeyContactEntry.CONTACT_ID);
		final String displayName = callerIntent.getStringExtra(KeyContactEntry.CONTACT_DISPLAY_NAME);
		final String phoneNumber = callerIntent.getStringExtra(KeyContactEntry.CONTACT_PHONE_NUMBER);
		final String emailAddres = callerIntent.getStringExtra(KeyContactEntry.CONTACT_EMAIL_ADDRESS);
		final String keyHash = callerIntent.getStringExtra(KeyContactEntry.KEY_HASH_VALUE);
		final int phoneInt = Integer.parseInt(phoneNumber);
		Log.i(TAG, "Presi i campi dal caller intent");
		
		TextView nameView = (TextView) findViewById(R.id.textContactDisplayName);
		nameView.setText(displayName);
		
		EditText phoneEdit = (EditText) findViewById(R.id.editChoosenPhone);
		phoneEdit.setText(phoneNumber);
		
		EditText emailEdit = (EditText) findViewById(R.id.editChoosenMail);
		emailEdit.setText(emailAddres);
		
		EditText hashEdit = (EditText) findViewById(R.id.editGeneratedHash);
		hashEdit.setText(keyHash);
		
		Log.i(TAG, "Messi i valori nei campi");
		
		ImageButton messageButton = (ImageButton) findViewById(R.id.imageSMSButton);
		messageButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent it = new Intent(Intent.ACTION_VIEW); 
				it.putExtra("address", phoneInt); 
				it.setType("vnd.android-dir/mms-sms");
				startActivity(it);
			}
		});
		
		ImageButton mailButton = (ImageButton) findViewById(R.id.imageMailButton);
		mailButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
					Key key = ks.getKey(displayName, null);
					Intent mailFragmentIntent = new Intent();
					
				} catch (KeyStoreException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnrecoverableKeyException e) {
					Toast.makeText(getApplicationContext(), "Chiave non trovata, keystore corrotto", Toast.LENGTH_LONG).show();
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_contact_view, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_contact_view,
					container, false);
			return rootView;
		}
	}

}
